package com.yaojiaxun.logisticsPlatformSystem.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PersonInformationDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.PersonInformation;
import com.yaojiaxun.logisticsPlatformSystem.mapper.PersonInformationMapper;
import com.yaojiaxun.logisticsPlatformSystem.util.MD5Util;
import com.yaojiaxun.logisticsPlatformSystem.util.SnowFlakeIDWorker;
import com.yaojiaxun.logisticsPlatformSystem.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: yjx_j
 * @Date: 2018/12/29 21:17
 * @Description:
 */
@Service
public class PersonInformationService {
    @Autowired
    PersonInformationMapper mapper;

//    //新增人员信息
//    public boolean insertPersonInformation(PersonInformation personInformation){
//        personInformation.setPersonId(new SnowFlakeIDWorker(1,1).nextId());
//        personInformation.setCreateTime(Instant.now().toEpochMilli());
//        personInformation.setUpdateTime(Instant.now().toEpochMilli());
//        return mapper.insert(personInformation)>0;
//    }

    //添加人员
    public Boolean insertPerson(PersonInformation personInformation){
        personInformation.setPersonId(new SnowFlakeIDWorker(1,1).nextId());
        personInformation.setCreateTime(Instant.now().toEpochMilli());
        personInformation.setUpdateTime(Instant.now().toEpochMilli());
        //初始密码默认设为123456
//        personInformation.setPassword(MD5Util.inputPassToDbPass("123456"));
        personInformation.setPassword(MD5Util.inputPassToDbPass(personInformation.getPassword()));
        return mapper.insertPerson(personInformation)>0;
    }

    //通过人员账号查找
    public PersonInformation findPersonByPersonAccount(String personAccount){
        PersonInformation person = mapper.findByPersonAccount(personAccount);
        return person;
    }

    //查找所有人员
    public List<PersonInformationDTO> findAllPerson(){
//        return mapper.findAllPerson();
        List<PersonInformation> list = mapper.findAllPerson();
        List<PersonInformationDTO> results = new ArrayList<>();
        for (PersonInformation person : list){
            PersonInformationDTO personInformationDTO = new PersonInformationDTO();
            BeanUtils.copyProperties(person, personInformationDTO);
            personInformationDTO.setUpdateTime(TimeFormat.convertTime(person.getUpdateTime()));
            personInformationDTO.setCreateTime(TimeFormat.convertTime(person.getCreateTime()));
            results.add(personInformationDTO);
        }
        return results;
    }

    //bean对象传入DTO
    public PersonInformationDTO beanToDTO(PersonInformation personInformation){
        if (personInformation == null)
            return null;
        PersonInformationDTO personInformationDTO = new PersonInformationDTO();
        BeanUtils.copyProperties(personInformation,personInformationDTO);
        personInformationDTO.setUpdateTime(TimeFormat.convertTime(personInformation.getUpdateTime()));
        personInformationDTO.setCreateTime(TimeFormat.convertTime(personInformation.getCreateTime()));
        return personInformationDTO;
    }

    //分页查询所有人员
    public PageDTO<PersonInformationDTO> findPage(Integer pageNum){
        PageDTO<PersonInformationDTO> pageDTO = new PageDTO<>();
        Page<PersonInformationDTO> page = new Page<>(pageNum, 10);
        List<PersonInformationDTO> result = mapper.selectPage(page,null).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setPage(page);
        pageDTO.setResult(result);
        return pageDTO;
    }

    //通过id查询人员
    public PersonInformationDTO findPersonById(Long id){
        PersonInformation personInformation = mapper.findByPersonId(id);
        return beanToDTO(personInformation);
    }

    //更新人员信息
    public Boolean updatePerson(PersonInformation personInformation){
        personInformation.setUpdateTime(Instant.now().toEpochMilli());
//        mapper.updateByPersonId(personInformation);
        return mapper.updateByPersonId(personInformation)>0;
    }

    //删除人员
    public Boolean deletePerson(Long id){
        return mapper.deleteByPersonId(id)>0;
    }

    public List<PersonInformationDTO> findPersonByName(String name){
        List<PersonInformation> list = mapper.findByPersonName(name);
        List<PersonInformationDTO> results = new ArrayList<>();
        for (PersonInformation person : list){
            PersonInformationDTO personInformationDTO = new PersonInformationDTO();
            BeanUtils.copyProperties(person, personInformationDTO);
            personInformationDTO.setUpdateTime(TimeFormat.convertTime(person.getUpdateTime()));
            personInformationDTO.setCreateTime(TimeFormat.convertTime(person.getCreateTime()));
            results.add(personInformationDTO);
        }
        return results;
    }
}
